#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"


void QueueInit(Queue* pq)
{
	assert(pq);
	pq->front = pq->rear = NULL;
}

void QueueDestory(Queue* pq)
{
	assert(pq);
	QueueNode* cur = pq->front;
	while (cur)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}
	pq->rear = pq->front = NULL;
}

void QueuePush(Queue* pq, QueuDateType x)
{
	assert(pq);
	QueueNode* newnode = (QueueNode*)malloc(sizeof(QueueNode));
	assert(newnode);
	newnode->date = x;
	newnode->next = NULL;
	if (pq->front == NULL && pq->rear == NULL)
	{
		pq->front = pq->rear = newnode;
	}

	else
	{
		pq->rear->next = newnode;
		pq->rear = newnode;
	}
}

void QueuePop(Queue* pq)
{
	assert(pq);
	if (pq->front == NULL && pq->rear == NULL)
	{
		return;
	}
	else if ((pq->front == pq->rear) && pq->front != NULL)
	{
		free(pq->front);
		pq->front = pq->rear = NULL;
	}
	else
	{
		//assert(pq->front);
		QueueNode* cur = pq->front;
		pq->front = pq->front->next;
		free(cur);
		cur = NULL;
	}
}

bool QueueEmpty(Queue* pq)
{
	assert(pq);
	return pq->front == NULL && pq->rear == NULL;
}

size_t QueueSize(Queue* pq)
{
	assert(pq);
	size_t n = 0;
	QueueNode* cur = pq->front;
	while (cur)
	{
		n++;
		cur = cur->next;
	}
	return n;
}

QueuDateType QueueFront(Queue* pq)
{
	assert(pq);
	assert(pq->front);
	return pq->front->date;
	
}

QueuDateType QueueBack(Queue* pq)
{
	assert(pq);
	assert(pq->rear);
	return pq->rear->date;
}